package net.gz01.wallet.web.controller.admin;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.jfinal.plugin.activerecord.Page;

import net.gz01.util.ZODate;
import net.gz01.wallet.common.PageRespon;
import net.gz01.wallet.common.ZOIDGenerate;
import net.gz01.wallet.common.ZOSearchCondition;
import net.gz01.wallet.web.controller.BaseController;
import net.gz01.wallet.web.model.Account;
import net.gz01.wallet.web.service.ReportService;

public class AccountController extends BaseController {
	public void search() {
		render("basedata/accountList.html");
	}

	public void listJson() {
		ZOSearchCondition sc = super.getSearchCondition();
		Page<Account> pageResult = Account.dao.search(sc);
		renderJson(new PageRespon(pageResult));
	}

	public void tempSearch() {
		render("basedata/tempAccountList.html");
	}

	public void listTempJson() {
		ZOSearchCondition sc = super.getSearchCondition();
		Page<Account> pageResult = Account.dao.tempSearch(sc);
		renderJson(new PageRespon(pageResult));
	}

	public void addTempPage() {
		render("basedata/tempAccountAdd.html");
	}

	public void addTemp() {
		String accountName = getPara("accountName");
		String accountNo = "T" + ZOIDGenerate.getNextMerchCode(true);
		Account ac = new Account();
		ac.setAccountNo(accountNo);
		ac.setAccountName(accountName);
		ac.setBalance(0L);
		ac.setFreezeAmount(0L);
		ac.setEnable(true);
		ac.setCreateDateTime(ZODate.getDateTime());
		ac.setTemp(true);
		ac.save();
		renderSuccess();
	}

	public void changeEnable() {
		Account.dao.changeEnable(getPara("id"));
		renderSuccess(false);
	}

	public void download() {
		ReportService reportService = new ReportService();
		File file = null;
		List<String> filePath = new ArrayList<>();
		{
			file = reportService.virtualBalance(filePath, null);
		}
		if (filePath == null || filePath.isEmpty()) {
			renderFail("没有满足条件的数据");
		} else {
			renderSuccess("/upload/report/tmp/" +filePath.get(0));
		}
	}

}
